<?php echo $this->doctype() ?>
<?php echo $this->headTitle('Панель управления') ?>
<?php echo $this->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')->appendHttpEquiv('Content-Language', 'ru-RU') ?>
<?php echo $this->headLink()->appendStylesheet('/css/cp/main.css', 'screen') ?>
<!--[if IE]><link rel="stylesheet" href="/css/cp/ie-main.css" type="text/css" /><![endif]-->
<script type="text/javascript" src="/js/closure/goog/base.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.js"></script>
<script type="text/javascript">
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.style');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.fx');
goog.require('goog.fx.dom');
goog.require('goog.net.XhrIo');
</script>
<div class="box-container" style="width: 330px">
  <div class="box-header">
    <h1>Авторизация</h1>
  </div>
  <div class="box-body">
    <div class="box-message" id="message" style="display: none"></div>
    <div class="auth-form">
      <?php echo $this->auth_form->render() ?>
    </div>
  </div>
    <div class="box-controls-wrap">
      <div class="box-controls">
        <div class="right">
        	<div class="btn"><button id="enter-login" onclick="Auth.init(this); return false;" class="primary">Войти</button></div>
        </div>
        <div id="progress" class="progress ajax-preloader-base"></div>
        <div id="control_text" class="box-controls-text"></div>
        <div class="clear-fix"></div>
      </div>
    </div>
   </div>

<script type="text/javascript">
var message = goog.dom.$('message'),
	progress = goog.dom.$('progress'),
	control_text = goog.dom.$('control_text');
goog.dom.$('auth_login').focus();
// hack
$('#cp-auth-form').append('<input type="submit" style="visibility: hidden;" value=""/>').parent().submit(function(){
	Auth.init($('#enter-login'));
	return false;
});
var Auth = {
	"init": function(el) {
		goog.style.showElement(message, false);
		this.showPreloader();
		var login = goog.dom.$('auth_login'),
			password =  goog.dom.$('auth_password');
		if (!login.value || !password.value) {
			message.innerHTML = '<p>Пожалуйста, укажите <b>логин</b> и <b>пароль</b>.</p>';
			fx = new goog.fx.dom.FadeInAndShow(message, 200);
			fx.play();
			this.hidePreloader();
			if (!login.value) {
				login.focus();
			} else {
				password.focus();
			}
			return;
		}
		el.disabled = true;
		login.disabled = true;
		password.disabled = true;
		goog.dom.classes.add(el, 'disabled');
		_self = this;
		goog.net.XhrIo.send('/cp/auth/login', function(response){
			var data = response.target.getResponseJson();
			message.innerHTML = '';
			if (typeof data.error == "object") {
				var ul = goog.dom.createDom('ul', {"id": "error_list"});
				goog.dom.append(message, ul);
				var error_list = goog.dom.$('error_list', null, message);
				goog.object.forEach(data.error, function(obj, from){
					goog.object.forEach(obj, function(message_error){
						var li = goog.dom.createDom('li', {}, message_error);
						goog.dom.append(error_list, li);
					});
				});
				fx = new goog.fx.dom.FadeInAndShow(message, 200);
				fx.play();
				el.disabled = false;
				login.disabled = false;
				password.disabled = false;
				_self.hidePreloader();
				goog.dom.classes.remove(el, 'disabled');
				return;
			}
			window.location.pathname = data.redirect;
		}, 'POST', "auth_login="+login.value+"&auth_password="+password.value);
	},
	"showPreloader": function() {
		goog.style.showElement(control_text, false);
		goog.style.setStyle(progress, 'display', 'block');  
	},
	"hidePreloader": function() {
		goog.style.showElement(control_text, true);
		goog.style.setStyle(progress, 'display', ''); 
	}
}
</script>
